<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

$this->useClass("Handler_Community");
class Action_Community_Themes extends Handler_Community {
  public function process() {
		$aa = $this->aa;
		$acl_data = $aa->getInfo();
		if (!$aa->is_granted('community_edit')) return;

		// -- old theme HACK : 2008-05-21 : lucky -- {{{
		$community_skin = 'platonica';
		$tpl = $this->ctx->tpl;
		$community_model = $this->ctx->mm->getModel("Community");
		$community_model->setDefaultSkin($community_skin);
		$tpl->setSkin($community_skin);
		$views_php = $tpl->findFile('', 'views', $tpl->_skin, 'php');
		unset ($config);
		include $views_php; // got $config
		$this->ctx->rh->views->setViewConfig($config);
		// -- old theme HACK : 2008-05-21 : lucky -- }}}
    
    $view = $this->rh->views->findByName('add_theme');
    $view->data = $aa->template_data;
    
    $tdi_nodes = $this->aa->getCommunityTdi($view->data['community']['oid']);
    if ( !empty($tdi_nodes) ) {
      $tdi_oid = array();
      foreach ($tdi_nodes as $node) {
        $tdi_oid[] = $node['id_object'];
      }
      
      $tdi_model = $this->ctx->mm->getModel("Tdi", "m");
      $tdi = $tdi_model->getSections($tdi_oid);
      
      $view->data['tdi_list'] = $tdi;
    }
    
    if ( isset($this->rh->POST) && !empty($this->rh->POST) ) {
      $info['title'] = htmlspecialchars($this->rh->POST['title']);
      $info['parent_oid'] = $this->rh->POST['feed']+0;
      $info['community_oid'] = $acl_data['comity_oid']+0;
      
      if ( isset($this->rh->POST['oid']) && !empty($this->rh->POST['oid']) ) {
        $theme = $this->ctx->store->create('ftheme', $info, $this->rh->POST['oid']);
        $theme->save();
      
    		$theme->updateSense("theme", $this->rh->POST['oid']);
    		$theme->save();
        $this->rh->setMessage('Тема обновлена.');
      } else {
        $theme = $this->ctx->store->create('ftheme', $info);
        $theme->save();
      
    		$theme->inSense("theme");
    		$theme->save();
        $this->rh->setMessage('Новая тема создана.');
      }
      
      $location = "{$this->aa->acl_data['base_url']}/{$acl_data['community_url']}/themes_list";
      if ( isset($this->rh->POST['lpage']) && !empty($this->rh->POST['lpage']) ) {
        $lpage = (integer) abs($this->rh->POST['lpage']+0);
        if ( $lpage > 0 ) {
          $location .= "?lpage={$lpage}";
        }
      } else if ( isset($this->rh->POST['show_all']) && !empty($this->rh->POST['show_all']) ) {
        $location .= '?show_all=1';
      }
      header('Location: ' . $location);
      exit;
    } else if ( isset($this->params['oid']) && !empty($this->params['oid']) ) {
      $object = $this->ctx->store->getObject($this->params['oid']);
      
      if ( isset($object->data) && !empty($object->data) ) {
        $view->data['item'] = $object->data;
        $view->data['item']['oid'] = $object->id;
        
        if ( isset($view->data['tdi_list']) && !empty($view->data['tdi_list']) ) {
          foreach ( $view->data['tdi_list'] as &$item ) {
            if ( $item['oid'] == $view->data['item']['parent_oid'] ) {
              $item['current'] = true;
            }
          }
        }
      }
      
      $rssmodel = $this->ctx->mm->getModel("RssFeed", "m");
      $rssmodel->setDisablePages();
      $view->data['rssfeed'] = $rssmodel->getRssFeedList($acl_data['comity_oid'], $this->params['oid']);
			
			foreach ( $view->data['rssfeed'] as &$feed ) {
				$feed['title'] = mb_ereg_replace('\r', ' ', $feed['title']);
				$feed['title'] = mb_ereg_replace('\n', ' ', $feed['title']);
			}
      
      $themesmodel = $this->ctx->mm->getModel("Themes", "m");
      
      $view->data['filters_list'] = $themesmodel->getFilters($this->params['oid'], true);
    }
      
    if ( isset($this->params['lpage']) && !empty($this->params['lpage']) ) {
      $view->data['lpage'] = (integer) abs($this->params['lpage']+0);
    }
    if ( isset($this->params['show_all']) && !empty($this->params['show_all']) ) {
      $view->data['show_all'] = 1;
    }
    
    $view->data['themes_tab'] = true;
    
    $this->rh->response->setContent($view);
  }
}